﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HalconDotNet;
using MachineVision.Core;
using MachineVision.Core.CircleMeasure;
using MachineVision.Core.OCR;
using MachineVision.Core.TemplateMatch.Shared;
using MachineVision.Shared.Controls;
using Microsoft.Win32;
using Prism.Commands;

namespace MachineVision.ObjectMeasure.ViewModels
{
    internal class CircleMeasureViewModel : NavigationViewModel
    {
        public CircleMeasureViewModel(CircleMeasureService circleMeasureService)
        {
            CircleMeasureService = circleMeasureService;
            DrawInfos = new ObservableCollection<DrawingObjectInfo>();
        }

        private void ExecuteLoadImageCommand()
        {
            Image?.Dispose();
            DrawInfos?.Clear();
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Multiselect = false;
            if (ofd.ShowDialog() == true)
            {
                if (File.Exists(ofd.FileName))
                {
                    try
                    {
                        HObject tmpimage;
                        HOperatorSet.GenEmptyObj(out tmpimage);
                        HOperatorSet.ReadImage(out tmpimage, ofd.FileName);
                        Image = tmpimage;
                        //Result.Message = $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}:图像加载完成";
                    }
                    catch (Exception)
                    {
                        // MatchResult.Message = $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}:加载图像失败,该图像不受支持";
                    }
                }
            }
        }

        private void ExecuteRunCommand()
        {
            try
            {
                var circlemodel = DrawInfos.FirstOrDefault(x => x.ShapeType == ShapeType.Circle);
                if (circlemodel == null)
                    throw new Exception("未创建卡尺模板");
                CircleMeasureService.RunParameter.Row = circlemodel.HTuples[0];
                CircleMeasureService.RunParameter.Column = circlemodel.HTuples[1];
                CircleMeasureService.RunParameter.Radius = circlemodel.HTuples[2];
                CircleMeasureService.Run(Image);
            }
            catch (Exception ex)
            {
                CircleMeasureService.MatchResult.Message =
                    $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}:运行出错,{ex.Message}";
            }
        }

        private ObservableCollection<DrawingObjectInfo> drawInfos;

        /// <summary>
        /// 绘制的图像集合
        /// </summary>
        public ObservableCollection<DrawingObjectInfo> DrawInfos
        {
            get { return drawInfos; }
            set
            {
                drawInfos = value;
                RaisePropertyChanged();
            }
        }

        private HObject image;

        /// <summary>
        /// 加载的图像
        /// </summary>
        public HObject Image
        {
            get { return image; }
            set
            {
                image = value;
                RaisePropertyChanged();
            }
        }

        private DelegateCommand loadImageCommand;
        public DelegateCommand LoadImageCommand =>
            loadImageCommand ?? (loadImageCommand = new DelegateCommand(ExecuteLoadImageCommand));

        private DelegateCommand runCommand;
        public DelegateCommand RunCommand =>
            runCommand ?? (runCommand = new DelegateCommand(ExecuteRunCommand));

        public CircleMeasureService CircleMeasureService { get; }
    }
}
